GrƔficos
A GramƔtica de GrƔficos
library(knitr)
library(tidyverse)
library(nycflights13)
flights %>% filter(dest=="DEN") %>%
ggplot() +
geom_point(aes(x=dep_time, y=dep_delay), size=1)

EstƩticas
Geometrias
Geometria para uma variÔvel discreta (o número de observações por grupo)
flights %>% ggplot() +
geom_bar(aes(x=origin))

Habilidade BÔsica de Programação: Apresentando GrÔficos no relatório final
flights %>% ggplot() +
geom_bar(aes(x=origin))

Parâmetros fixos
flights %>%
ggplot() +
geom_histogram(aes(x=velocidade), binwidth=1)

flights %>%
ggplot() +
geom_histogram(aes(x=velocidade), binwidth=1, colour="black", fill="orange")

GrĆ”ficos de duas variĆ”veis contĆnuas
flights %>% sample_n(1000) %>%
ggplot() +
geom_point(aes(x = distance, y = air_time))

flights %>% sample_n(1000) %>%
ggplot() +
geom_point(aes(x = distance, y = air_time), size=0.1, color="blue", shape=2)

flights %>% sample_n(1000) %>%
ggplot() +
geom_point(aes(x = distance, y =air_time), size=0.1) +
geom_smooth(aes(x = distance, y = air_time), method = "lm", se = FALSE)

flights %>%# sample_n(50) %>%
ggplot() +
geom_point(aes(x = distance, y = air_time), size=0.1) +
geom_smooth(aes(x = distance, y = air_time), method = "lm")

flights %>% sample_n(1000) %>% ggplot() +
geom_point(aes(x = distance, y = air_time), size=0.1) +
geom_smooth(aes(x = distance, y = air_time), method = "loess")

GrÔficos de três ou mais variÔveis
flights %>% sample_n(1000) %>% ggplot() +
geom_point(aes(x = dep_time, y = dep_delay, size=distance))

flights %>% sample_n(1000) %>% ggplot() +
geom_point(aes(x = dep_time, y = dep_delay, colour=origin))

flights %>% sample_n(1000) %>% ggplot() +
geom_point(aes(x = dep_time, y = dep_delay, shape=origin))

Múltiplos GrÔficos (facet_grid)
flights %>% sample_n(1000) %>%
ggplot() +
geom_point(aes(x = dep_time, y = dep_delay)) +
facet_grid(cols=vars(origin))

flights %>% sample_n(1000) %>%
ggplot() +
geom_point(aes(x = dep_time, y = dep_delay)) +
facet_grid(rows=vars(origin))

flights %>% sample_n(1000) %>%
ggplot() +
geom_point(aes(x = dep_time, y = dep_delay)) +
facet_grid(rows=vars(month), cols=vars(origin))

GrƔficos de Linha
flights %>%
mutate(month=factor(month, levels=1:12, ordered=TRUE)) %>%
group_by(month) %>%
summarize(dep_delay_media=mean(dep_delay,na.rm=T)) %>%
ggplot() +
geom_line(aes(x=month, y=dep_delay_media), group=1)

flights %>%
mutate(month=factor(month, levels=1:12, ordered=TRUE)) %>%
group_by(month, origin) %>%
summarize(dep_delay_media=mean(dep_delay,na.rm=T)) %>%
ggplot() +
geom_line(aes(x=month, y=dep_delay_media, group=origin))

flights %>%
mutate(month=factor(month, levels=1:12, ordered=TRUE)) %>%
group_by(month, origin) %>%
summarize(dep_delay_media=mean(dep_delay,na.rm=T)) %>%
ggplot() +
geom_line(aes(x=month, y=dep_delay_media, group=origin, colour=origin))

GrƔfico de barras 100%
flights %>%
mutate(month=factor(month, levels=1:12, ordered=TRUE)) %>%
group_by(month, origin) %>%
summarize(dep_delay_total=sum(dep_delay,na.rm=T)) %>%
ggplot() +
geom_col(aes(x=month, y=dep_delay_total, fill=origin), position = "fill")

Mais geometrias
flights %>% sample_n(100) %>%
ggplot() +
geom_text(aes(x = dep_time, y = dep_delay, label=dest))

flights %>% group_by(origin, month) %>%
summarize(dep_delay_media=mean(dep_delay,na.rm=T)) %>%
ggplot() +
geom_tile(aes(x = origin, y = month, fill=dep_delay_media))

flights %>% group_by(origin) %>%
summarize(dep_delay_total=sum(dep_delay, na.rm=T)) %>%
ggplot() +
geom_col(aes(x="", y=dep_delay_total, fill=origin), position="fill")

flights %>% group_by(origin) %>%
summarize(dep_delay_total=sum(dep_delay, na.rm=T)) %>%
ggplot() +
geom_col(aes(x="", y=dep_delay_total, fill=origin), position="fill") +
coord_polar(theta="y")

Personalização de GrÔficos além de geometria
flights %>% sample_n(1000) %>% ggplot() +
geom_point(aes(x = distance, y = air_time, color=origin)) +
ggtitle("Relação entre distância e duração de voo, por aeroporto do Nova Iorque")

flights %>% sample_n(1000) %>% ggplot() +
geom_point(aes(x = distance, y = air_time, color=origin)) +
ggtitle("Relação entre distância e duração de voo, por aeroporto do Nova Iorque") +
xlab("Distância") +
ylab("Duração")

flights %>% sample_n(1000) %>% ggplot() +
geom_point(aes(x = distance, y = air_time, color=origin)) +
ggtitle("Relação entre distância e duração de voo, por aeroporto do Nova Iorque") +
xlab("Distância") +
ylab("Duração") +
theme(legend.position="bottom")

flights %>% sample_n(1000) %>% ggplot() +
geom_point(aes(x = distance, y = air_time, color=origin)) +
ggtitle("Relação entre distância e duração de voo, por aeroporto do Nova Iorque") +
xlab("Distância") +
ylab("Duração") +
theme(axis.text.x = element_text(size=4),
axis.text.y = element_text(size=4),
axis.title.x = element_text(size=4),
axis.title.y = element_text(size=4))

flights %>% sample_n(1000) %>% ggplot() +
geom_point(aes(x = distance, y = air_time, color=origin)) +
ggtitle("Relação entre distância e duração de voo, por aeroporto do Nova Iorque") +
xlab("Distância") +
ylab("Duração") +
theme_classic()

# install.packages("ggthemes")
library(ggthemes)
flights %>% sample_n(1000) %>% ggplot() +
geom_point(aes(x = distance, y = air_time, color=origin)) +
ggtitle("Relação entre distância e duração de voo, por aeroporto do Nova Iorque") +
xlab("Distância") +
ylab("Duração") +
theme_economist()

flights %>% ggplot() +
geom_bar(aes(x=origin))

ggsave("flights_barplot.png")
GrƔficos interativas e animaƧƵes
library(plotly)
graf_1 <- flights %>% sample_n(1000) %>% ggplot() +
geom_point(aes(x = distance, y = air_time, color=origin)) +
ggtitle("Relação entre distância e duração de cada voo, por aeroporto do Nova Iorque em 2013") +
xlab("Distância") +
ylab("Duração") +
theme_classic()
graf_1 %>%
ggplotly()
graf_2 <- flights %>% sample_n(1000) %>% ggplot() +
geom_point(aes(x = distance, y = air_time, color=origin, frame=month)) +
ggtitle("Relação entre distância e duração de cada voo, por aeroporto do Nova Iorque em 2013") +
xlab("Distância") +
ylab("Duração") +
theme_classic()
graf_2 %>%
ggplotly()